package com.gitee.dupe.file.service.impl;

import com.gitee.dolt.boot.service.SuperServiceImpl;
import com.gitee.dolt.core.util.BizAssert;
import com.gitee.dupe.file.dao.AttachmentMapper;
import com.gitee.dupe.file.entity.Attachment;
import com.gitee.dupe.file.service.IAttachmentService;
import com.gitee.dupe.file.strategy.FileStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LYH
 * @since 2020-10-11
 */
@Service
@RequiredArgsConstructor
public class AttachmentServiceImpl extends SuperServiceImpl<AttachmentMapper, Attachment> implements IAttachmentService {

    private final FileStrategy fileStrategy;

    @Override
    public Attachment upload(MultipartFile multipartFile) {
        Attachment attachment = fileStrategy.upload(multipartFile);
        save(attachment);
        return attachment;
    }

    @Override
    public void download(HttpServletRequest request, HttpServletResponse response, List<Long> ids) {
        List<Attachment> attachments = listByIds(ids);
        BizAssert.notEmpty(attachments, "文件未找到");
        fileStrategy.download(request, response, attachments);
    }

    @Override
    public void handlerDelete(List<Long> ids) {
        List<Attachment> attachments = listByIds(ids);
        removeByIds(ids);
        fileStrategy.delete(attachments);
    }
}
